package com.m3839.sdk.anti.ui;

import android.os.Bundle;

import com.m3839.sdk.anti.js.RealNameJsInterface;
import com.m3839.sdk.common.base.BaseJsInterface;
import com.m3839.sdk.common.base.BaseWebDialogFragment;
import com.m3839.sdk.common.core.ApiError;
import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.core.ErrorConstant;
import com.m3839.sdk.common.env2.ApiManager;
import com.m3839.sdk.common.event.SubjectBus;

/**
 * @author chenbaoyang
 */
public class RealNameDialogFragment extends BaseWebDialogFragment {

    private String uid;
    private String loginType;
    private String token;

    public static RealNameDialogFragment newInstance(String uid, String loginType, String token) {
        RealNameDialogFragment fragment = new RealNameDialogFragment();
        Bundle data = new Bundle();
        data.putString(Constant.HYKB_KEY_USER_ID, uid);
        data.putString(Constant.HYKB_KEY_USER_LOGIN_TYPE, loginType);
        data.putString(Constant.HYKB_KEY_USER_TOKEN, token);
        fragment.setArguments(data);
        return fragment;
    }

    @Override
    protected void parseArgs() {
        uid = getArguments().getString(Constant.HYKB_KEY_USER_ID);
        loginType = getArguments().getString(Constant.HYKB_KEY_USER_LOGIN_TYPE);
        token = getArguments().getString(Constant.HYKB_KEY_USER_TOKEN);
    }

    @Override
    protected void setWebContent() {
        RealNameJsInterface jsInterface = new RealNameJsInterface(getActivity());
        webView.addJavascriptInterface(jsInterface, BaseJsInterface.JS_INTERFACE_NAME);
        String url = ApiManager.getInstance().buildWebRealNameUrl(uid, loginType, token);
        webView.loadUrl(url);
    }

    @Override
    protected boolean isInterceptBackEvent() {
        dismiss();
        ApiError apiError = new ApiError(ErrorConstant.HYKB_CODE_DIALOG_ANTI_CLOSE,
                ErrorConstant.ERR_MSG_SDK_DIALOG_ANTI_CLOSE);
        SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_CALLBACK,apiError);
        return super.isInterceptBackEvent();
    }

}
